﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace net_ddns_server_ali.Code
{
    public class NewConsoleLog : ConsoleLog
    {
        public NewConsoleLog(string name) : base(name)
        {

        }

        protected override void OnWriteLine(string level, string message)
        {
            ConsoleColor foregroundColor = ConsoleColor.White;
            ConsoleColor backgroundColor = ConsoleColor.Black;
            switch (level.ToLower())
            {
                case "{emtpy}":
                    foregroundColor = ConsoleColor.Gray;
                    break;
                case "{debug}":
                    foregroundColor = ConsoleColor.Green;
                    break;
                case "{warning}":
                    foregroundColor = ConsoleColor.Yellow;
                    break;
                case "{error}":
                    foregroundColor = ConsoleColor.Red;
                    break;
            }

            DateTime now = DateTime.Now;
            ConsoleColor foregroundColor2 = Console.ForegroundColor;
            ConsoleColor backgroundColor2 = Console.BackgroundColor;
            Console.ForegroundColor = foregroundColor;
            Console.BackgroundColor = backgroundColor;

            string log = $"{now:yyyy-MM-dd HH:mm:ss} {Name}\t{message}";

            if (!Directory.Exists("log"))
            {
                Directory.CreateDirectory("log");
            }

            Console.WriteLine(log);
            File.AppendAllText($"log{Path.DirectorySeparatorChar}{now:yyyyMMdd}.log", $"{log}\r\n");

            Console.ForegroundColor = foregroundColor2;
            Console.BackgroundColor = backgroundColor2;
        }
    }
}
